﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Dtos;
using Core.Domain;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity;
using Core.Messages;
using Core.Mappers;
using AutoMapper;
using AutoMapper.Mappers;
using Core.Context;
using Core.ViewModels;

namespace DI
{
    public static class AutoMapperConfig
    {
        private static bool s_isInit = false;

        public static void CreateMappings()
        {
            if (s_isInit)
                return;
            s_isInit = true;            

            Mapper.CreateMap<Role, RoleDto>();
            Mapper.AddProfile<ListResponseProfile<Role, RoleDto>>();
            Mapper.CreateMap<RoleDto, Role>()
                      .ForMember(x => x.Id, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.BrokenRules, opt => opt.Ignore())
                      .ForMember(x => x.Users, opt => opt.Ignore());

            Mapper.CreateMap<AppUser, UserDto>()
                      .ForMember(x => x.Password, opt => opt.Ignore());
            Mapper.AddProfile<ListResponseProfile<AppUser, UserDto>>();
            Mapper.CreateMap<UserDto, AppUser>()
                      .ForMember(x => x.Id, opt => opt.Condition(x => !x.IsSourceValueNull && !x.SourceValue.Equals(0)))
                      .ForMember(x => x.BrokenRules, opt => opt.Ignore())
                      .ForMember(x => x.TwoFactorEnabled, opt => opt.Ignore())
                      .ForMember(x => x.PhoneNumberConfirmed, opt => opt.Ignore())
                      .ForMember(x => x.EmailConfirmed, opt => opt.Ignore())
                      .ForMember(x => x.Roles, opt => opt.Ignore())
                      .ForMember(x => x.Claims, opt => opt.Ignore())
                      .ForMember(x => x.Logins, opt => opt.Ignore());            

            Mapper.CreateMap<UserLoginInfo, UserLoginInfoTo>();
            Mapper.CreateMap<UserLoginInfoTo, UserLoginInfo>();

            Mapper.AssertConfigurationIsValid();
        }
    }
}